package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.FocusUser;
import com.tanhua.dubbo.api.mongo.FocusUserApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;

@Service
public class FocusUserApiImpl implements FocusUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Long userId, Long focusUserId) {
        FocusUser focusUser = new FocusUser();
        focusUser.setId(new ObjectId());
        focusUser.setUserId(userId);
        focusUser.setFocusUserId(focusUserId);
        focusUser.setCreated(new Date().getTime());
        mongoTemplate.save(focusUser);
    }

    @Override // delete from biao where userId={userId} and focusUserId={}
    public void delete(Long userId, Long focusUserId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("focusUserId").is(focusUserId));
        mongoTemplate.remove(query,FocusUser.class);
    }
}
